<template>
  <div>
    <el-card>
      <div class="headBox">
        <el-button type="primary" @click="addlist" icon="el-icon-plus"
          >添加项目</el-button
        >
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="id" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目名称">
          <template slot-scope="scope">
            <span v-if="scope.row.state">{{ scope.row.name }}</span>
            <el-input
              v-else
              v-model="dataadd.input"
              placeholder="请输入内容"
              clearable
            />
          </template>
        </el-table-column>
        <el-table-column label="总平方米">
          <template slot-scope="scope">
            <span>{{ scope.row.square }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="模板名称" prop="templates_id__name">
        </el-table-column>
        <el-table-column label="操作" width="400px">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
            <el-button @click="goToArchite(scope.row)" size="mini"
              >数据</el-button
            >
            <el-button @click="goToStore(scope.row)" size="mini"
              >仓库</el-button
            >
            <el-button @click="goToStatistics(scope.row)" size="mini"
              >统计</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加弹框 -->
    <el-dialog title="添加" :visible.sync="dialogVisible" width="30%" :close-on-click-modal="false">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item
          label="项目名称"
          prop="name"
          :rules="[{ required: true, message: '姓名不能为空' }]"
        >
          <el-input v-model="form.name" class="addNameInp" />
        </el-form-item>
        <el-form-item
          label="模板"
          prop="value"
          :rules="[{ required: true, message: '模板不能为空' }]"
        >
          <el-select
            v-model="form.value"
            placeholder="请选择"
            class="addSelect"
          >
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑弹框 -->
    <el-dialog title="编辑" :visible.sync="dialogVisible1" width="30%" :close-on-click-modal="false">

      <el-form ref="form1" :model="form1" label-width="80px">
        <el-form-item
          label="项目名称"
          prop="name"
          :rules="[{ required: true, message: '姓名不能为空' }]"
        >
          <el-input v-model="form1.name" />
        </el-form-item>
        <!-- <el-form-item
          label="模板"
          prop="value"
          :rules="[{ required: true, message: '模板不能为空' }]"
        >
          <el-select v-model="form1.value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel1">取 消</el-button>
        <el-button type="primary" @click="confirm1">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getproject,
  postproject,
  delproject,
  putproject,
} from "../../api/project";
import { gettemplate } from "../../api/template";
export default {
  components: {},
  data() {
    return {
      tableData: [],
      form: {
        name: "",
        value: "",
      },
      form1: {
        id: "",
        name: "",
        // value: "",
      },
      options: [],
      dialogVisible: false,
      dialogVisible1: false,
    };
  },
  computed: {},
  created() {
    this.datalist();
    this.templatedata();
  },
  mounted() {},
  methods: {
    // 获取数据
    async datalist() {
      const data = await getproject();
      console.log(123332, data);
      if (data.code == 200) {
        data.data.forEach((item, index) => {
          item["state"] = true;
        });
        this.tableData = data.data;
      }
    },
    async templatedata() {
      const data = await gettemplate();
      if (data.code == 200) {
        this.options = data.data;
      }
    },
    // 编辑数据
    async handleEdit(index, datas) {
      this.dialogVisible1 = true;
      this.form1.id = datas.id;
      this.form1.name = datas.name;
      // this.form1.value = datas.templates_id;
    },
    // 编辑数据取消
    cancel1() {
      this.dialogVisible1 = false;
    },
    // 编辑数据确认
    confirm1() {
      this.dialogVisible1 = false;
      console.log(1111);

      this.$refs.form1.validate(async (valid) => {
        if (valid) {
          this.dialogVisible = false;
          const p = {
            id: this.form1.id,
            name: this.form1.name,
            // models: this.form1.value,、
          };
          const data = await putproject(p);
          if (data.code === 200) {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          }
          this.datalist();
          this.$refs.form.resetFields();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 删除数据
    handleDelete(index, datas) {
      this.$confirm("此操作将永久删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const data = await delproject({ id: datas.id });
          if (data.code === 200) {
            this.$message({
              type: "success",
              message: "项目删除成功!",
            });
            this.datalist();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    addlist() {
      this.dialogVisible = true;
    },
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.dialogVisible = false;
          console.log(this.form.name);
          const p = {
            name: this.form.name,
            models: this.form.value,
          };
          const data = await postproject(p);
          if (data.code === 200) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
          }
          this.datalist();
          this.$refs.form.resetFields();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cancel() {
      this.dialogVisible = false;
      this.$refs.form.resetFields();
    },
    goToArchite(row) {
      console.log(row);
      this.$router.push({
        name: "architectural",
        query: {
          template_id: row.templates_id,
          project_id: row.id,
          createTime: row.createTime,
        },
      });
    },
    // 去该项目下仓库
    goToStore(row) {
      console.log(row);
      this.$router.push({
        path: "/store/store",
        query: { id: row.id },
      });
    },
    goToStatistics(row){
      this.$router.push({
        path: "/Statistics",
        query: { project_id: row.id },
      });
    }
  },
};
</script>

<style scoped lang="scss">
.headBox {
  width: 100%;
  margin: 2vh;
  margin-left: 0;
}

.addNameInp,
.addSelect {
  width: 260px;
}
</style>
<style>
.el-dialog {
  min-width: 400px;
}
</style>
